建立一些模型,比較不同模型在不同的參數設定下對 iris dataset 的分類能力。
第一個模型未使用 Logistic Regression 邏輯斯迴歸,在統計的領域中也稱作 Logit Model,他是一種非線性的廻歸模型,
from sklearn.linear_model import LogisticRegression
# https://scikit-learn.org/stable/auto_examples/linear_model/plot_sparse_logistic_regression_mnist.html#sphx-glr-auto-examples-linear-model-plot-sparse-logistic-regression-mnist-py
# create classifier
clf = LogisticRegression(C=20, penalty="l2", solver="saga",\
multi_class='multinomial', tol=0.0001, random_state=20241006)
# train classifier
clf.fit(X_train, y_train)
# predict
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
# Evaluate the Model
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_train, y_pred_train))
print()
print(confusion_matrix(y_test, y_pred_test))
from sklearn.metrics import classification_report
print('\n Train ***********************************')
print('Accuracy: {:.2f}'.format(accuracy_score(y_train, y_pred_train)))
print('Confusion matrix = {}'.format(confusion_matrix(y_train, y_pred_train)))
print(confusion_matrix(y_train, y_pred_train))
print(classification_report(y_train, y_pred_train))
print('\n Test ************************************')
print('Accuracy: {:.2f}'.format(accuracy_score(y_test, y_pred_test)))
print(classification_report(y_test, y_pred_test)